package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.dto.BrandDTO;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageResult<BrandDTO> pageQuery(Integer page, Integer rows, String key, String sortBy, Boolean desc) {
        try {
            //创建商品条件查询对象
            Example example = new Example(Brand.class);
            //添加商品查询条件
            Example.Criteria criteria = example.createCriteria();
            //判断key是否为null值
            if(StringUtils.isNotBlank(key)){
                criteria.orEqualTo("letter",key);
                criteria.orLike("id","%"+key+"%");
                criteria.orLike("name","%"+key+"%");
            }
            //sortBy 对应的是点击的name或letter,根据这两个参数来判断是给哪个字段升序或降序
            if(StringUtils.isNotBlank(sortBy)){
                example.setOrderByClause(sortBy + (desc ? " desc":" asc"));
            }
            //设置分页信息
            PageHelper.startPage(page,rows);
            //查询所有品牌
            List<Brand> brands = brandMapper.selectByExample(example);
            //获取PageHelper分页对象
            PageInfo<Brand> pageInfo = new PageInfo<>(brands);
            //返回数据
            return new PageResult<>(pageInfo.getTotal(), BeanHelper.copyWithCollection(brands,BrandDTO.class));
        } catch (Exception e) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
    }

    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
        try {
            //把dto转成pojo
            Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
            brandMapper.insertSelective(brand);
            brandMapper.addBrandAndCategory(brand.getId(),cids);
        } catch (Exception e) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    public BrandDTO findById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if(brand == null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand,BrandDTO.class);
    }

    public List<BrandDTO> findByCategoryId(Long id) {
       List<Brand> brands = brandMapper.findByCategoryId(id);
       if(CollectionUtils.isEmpty(brands)){
           throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
       }
       return BeanHelper.copyWithCollection(brands,BrandDTO.class);
    }

    public List<BrandDTO> findByIds(List<Long> ids) {
        List<Brand> brands = brandMapper.selectByIdList(ids);
        if(CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brands, BrandDTO.class);
    }
}
